package com.power.using.mydaggerp.presenter.detail;

import android.support.annotation.NonNull;

import com.power.using.mydaggerp.contract.detail.WangyiDetailContract;
import com.power.using.mydaggerp.helper.JsonHelper;
import com.power.using.mydaggerp.model.bean.wangyi.WangyiNewsDetailBean;
import com.power.using.mydaggerp.model.detail.WangyiDetailModel;

import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;

/**
 * Created by 聪明一只哈 on 2019/10/12.
 * Time: 2019/10/12  13:44
 */
public class WangyiDetailPresenter extends WangyiDetailContract.WangyiDetailPresenter{

    @NonNull
    public static WangyiDetailPresenter newInstance() {
        return new WangyiDetailPresenter();
    }


    @Override
    public void loadNewsDetailWithUrl(String url) {
        if(mIView==null){
            return;
        }

        try {
            mIView.showNewsDetail(url);
        }catch (Exception e){
            mIView.showNetworkError();
            e.printStackTrace();
        }

    }

    @Override
    public void loadNewsDetailWithId(final String id) {
        mRxManage.register(mIModel.getNewsDetail(id).subscribe(new Consumer<ResponseBody>() {
            @Override
            public void accept(ResponseBody responseBody) throws Exception {
                if (mIView == null)
                    return;

                WangyiNewsDetailBean bean = JsonHelper.getNewsDetailBeans(responseBody.string(), id);
                mIView.showNewsDetail(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView==null)
                    return;

                mIView.showNetworkError();
            }
        }));

    }

    @Override
    protected WangyiDetailContract.IWangyiDetailModel getModel() {
        return WangyiDetailModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
